package com.example.experiment_three;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

public class ProgressDialogTest extends AppCompatActivity {

    private int[]data = new int[100];
    int hasData = 0;
    final int INCREASE = 0;
    final int PROGRESS_DIALOG = 0x112;
    int progressStatus = 0;
    ProgressDialog pd;
    Handler handler;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_progress_dialog_test);


        Button execBn = (Button) findViewById(R.id.exec);
        execBn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog(PROGRESS_DIALOG);
            }
        });
        //补全Handler代码
        handler = new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                switch(msg.what) {
                    case INCREASE:
                        pd.incrementProgressBy(1);
                        if(pd.getProgress() >= 100) {
                            //progressDialog.dismiss();
                        }
                        break;
                }
                super.handleMessage(msg);
            }
        };
    }
    @Override
    public Dialog onCreateDialog(int id,Bundle status){
        System.out.println("-----create-----");
        switch(id){
            case PROGRESS_DIALOG:
                pd = new ProgressDialog(this);
                pd.setMax(100);
                pd.setTitle("任务完成百分比");
                pd.setMessage("耗时任务的完成百分比");
                pd.setCancelable(false);
                pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                pd.setIndeterminate(false);
                break;
        }
        return pd;
    }

    @Override
    public void onPrepareDialog(int id,Dialog dialog) {
        System.out.println("-----prepare-----");
        super.onPrepareDialog(id, dialog);
        switch (id) {
            case PROGRESS_DIALOG:
                pd.incrementProgressBy(-pd.getProgress());
                new Thread() {
                    public void run() {
                        while (progressStatus < 100) {
                            progressStatus = doWork();
                            //补全Handler代码
                            handler.sendEmptyMessage(INCREASE);
                            if (progressStatus >= 100) {
                                pd.dismiss();
                            }
                        }

                    }
                }.start();
                break;
        }

    }

    public int doWork(){
        data[hasData++] = (int)(Math.random()*100);
        try{
            Thread.sleep(100);
        } catch (InterruptedException e){
            e.printStackTrace();
        }
        return hasData;
    }
}